package com.jfinal.weixin.sdk.api;

import java.util.HashMap;
import java.util.Map;

import com.jfinal.weixin.sdk.utils.HttpUtils;
import com.jfinal.weixin.sdk.utils.JsonUtils;

/*code解码接口支持两种场景： 1.商家获取choos_card_info后，将card_id和encrypt_code字段通过解码接口，获取真实code。 
2.卡券内跳转外链的签名中会对code进行加密处理，通过调用解码接口获取真实code。*/
public class DecryptCodeApi {

private static String apiurl = "https://api.weixin.qq.com/card/code/decrypt?access_token=";//获取卡券code
private static String appurl = "https://api.weixin.qq.com/card/code/get?access_token=" ;//批量卡券详情
private static String aplurl ="https://api.weixin.qq.com/card/get?access_token=";//单个卡券详情
private static String conurl ="https://api.weixin.qq.com/card/code/consume?access_token=";//核销卡券
	/**
	 * 获取卡券code
	 * @param encrypt_code
	 * @return
	 */
	public static String getCode(String encrypt_code){
		Map<String, Object> params = new HashMap<String, Object>();
		String access_token = AccessTokenApi.getAccessTokenStr();
		String url=apiurl+access_token;
		params.put("encrypt_code", encrypt_code);
		String code=HttpUtils.post(url,JsonUtils.toJson(params));
		return code;
	}
	/**
	 * 获取卡券详情
	 * @param code
	 * @return
	 */
	public static String getCardDetail(String card_id,String code,boolean check_consume){
		Map<String, Object> params = new HashMap<String, Object>();
		String access_token = AccessTokenApi.getAccessTokenStr();
		String url=appurl+access_token;
		params.put("card_id", card_id);
		params.put("code", code);
		params.put("check_consume", check_consume);
		String deail=HttpUtils.post(url,JsonUtils.toJson(params));
		return deail;
	}
	/**
	 * 获取单个卡券详情
	 * @param code
	 * @return
	 */
	public static String getCardInfoDetail(String card_id){
		Map<String, Object> params = new HashMap<String, Object>();
		String access_token = AccessTokenApi.getAccessTokenStr();
		String url=aplurl+access_token;
		params.put("card_id", card_id);
		String deail=HttpUtils.post(url,JsonUtils.toJson(params));
		return deail;
	}
	/**
	 * 核销卡券
	 * @param code
	 * @return
	 */
	public static String consumeCard(String card_id,String code){
		Map<String, Object> params = new HashMap<String, Object>();
		String access_token = AccessTokenApi.getAccessTokenStr();
		String url=conurl+access_token;
		params.put("card_id", card_id);
		params.put("code", code);
		String consume=HttpUtils.post(url,JsonUtils.toJson(params));
		return consume;
	}
}
